package com.kerbores.shiro.matcher;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.nutz.lang.Lang;

import com.kerbores.utils.encode.DES;

/**
 * DES加密验证器,使用用户名加密密码
 * 
 * @author 王贵源
 * @email kerbores@kerbores.com
 *
 *        create at 2015年10月20日 下午12:08:50
 */
public class KerboresDESCredentialsMatcher extends SimpleCredentialsMatcher {
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.shiro.authc.credential.SimpleCredentialsMatcher#doCredentialsMatch
	 * (org.apache.shiro.authc.AuthenticationToken,
	 * org.apache.shiro.authc.AuthenticationInfo)
	 */
	@Override
	public boolean doCredentialsMatch(AuthenticationToken authcToken, AuthenticationInfo info) {
		UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
		Object tokenCredentials = DES.encrypt(String.valueOf(token.getPassword()), Lang.md5(token.getUsername()));// 用用户名的MD5值加密密码
		Object accountCredentials = getCredentials(info);
		return equals(tokenCredentials, accountCredentials);
	}
}
